﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 文件夹文件更新检测
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 文本框最多存储8M内容
        /// </summary>
        private void richTextBoxLengthCheck()
        {
            if (richTextBox.TextLength > 1024 * 1024 * 8)
            {
                richTextBox.Clear();
            }
        }

        /// <summary>
        /// 当更改指定 Path 中的文件和目录时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                return;
            }
            richTextBoxLengthCheck();
            richTextBox.AppendText($"【{DateTime.Now:yyyy/MM/dd HH:mm:ss}】 {e.FullPath} 发生更改\r\n");
        }

        /// <summary>
        /// 当在指定 Path 中创建文件和目录时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            richTextBoxLengthCheck();
            richTextBox.AppendText($"【{DateTime.Now:yyyy/MM/dd HH:mm:ss}】 {e.FullPath} 创建\r\n");
        }

        /// <summary>
        /// 删除指定 Path 中的文件或目录时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            richTextBoxLengthCheck();
            richTextBox.AppendText($"【{DateTime.Now:yyyy/MM/dd HH:mm:ss}】 {e.FullPath} 删除\r\n");
        }

        /// <summary>
        /// 重命名指定 Path 中的文件或目录时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            richTextBoxLengthCheck();
            richTextBox.AppendText($"【{DateTime.Now:yyyy/MM/dd HH:mm:ss}】 {e.OldFullPath} 改名为 {e.FullPath}\r\n");
        }

        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SelectDirectory_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                fileSystemWatcher.Path = folderBrowserDialog.SelectedPath;
                fileSystemWatcher.NotifyFilter = NotifyFilters.Attributes
                                            | NotifyFilters.CreationTime
                                            | NotifyFilters.DirectoryName
                                            | NotifyFilters.FileName
                                            | NotifyFilters.LastAccess
                                            | NotifyFilters.LastWrite
                                            | NotifyFilters.Security
                                            | NotifyFilters.Size;
                richTextBoxLengthCheck();
                richTextBox.Text = $"【{DateTime.Now:yyyy/MM/dd HH:mm:ss}】 开始检测【{fileSystemWatcher.Path}】的修改操作\r\n";
                label_Path.Text = fileSystemWatcher.Path;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            fileSystemWatcher.Path = "./";
            fileSystemWatcher.NotifyFilter = NotifyFilters.Attributes
                                            | NotifyFilters.CreationTime
                                            | NotifyFilters.DirectoryName
                                            | NotifyFilters.FileName
                                            | NotifyFilters.LastAccess
                                            | NotifyFilters.LastWrite
                                            | NotifyFilters.Security
                                            | NotifyFilters.Size;


        }


        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispose();
            Close();
        }
    }
}
